package com.navi.loaddata.loadService.impl.hisData.cf;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mesEntity.MesCtProductlocationhist;
import com.navi.loaddata.core.mesEntity.MesProducthistory;
import com.navi.loaddata.core.oracleEntity.EtlGlassHstV;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.ProcessUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jay
 */
@Service
public class ProcessHisDataServiceCF2 {
    private static final Logger logger = LoggerFactory.getLogger(ProcessHisDataServiceCF2.class);
    @Autowired
    private MesRepositoryImpl mesdao;

    public EtlGlassHstV processHis(Map<String, MesProducthistory> hisTrackIn, MesProducthistory hisOut) throws Exception {
        EtlGlassHstV etlCfGlassHstV = new EtlGlassHstV();
        try {
            etlCfGlassHstV.setStepId(hisOut.getOldprocessoperationname());
            etlCfGlassHstV.setGlassId(hisOut.getProductname());
            etlCfGlassHstV.setGlassEndTime(hisOut.getEventtime());
//            if(null==hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey())){
//                throw new ETLException("errorProcessArrayGlassHstV:Linkkey IS NULL ");
//            }
            if(!hisTrackIn.isEmpty()&&null!=hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey())) {
                Date glassStartTime = hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey()).getEventtime();
                etlCfGlassHstV.setGlassStartTime(glassStartTime);
            }else{
                logger.error("glassld={} linkkey={} 没有对应的TrackIn数据!",hisOut.getProductname(),hisOut.getLinkkey());
            }
            etlCfGlassHstV.setProductId(hisOut.getProductspecname());
            etlCfGlassHstV.setProductVersion(hisOut.getProductspecversion());
            etlCfGlassHstV.setLotId(ProcessUtil.processString(hisOut.getLotname()));
            etlCfGlassHstV.setEquipId(hisOut.getMachinename());
            etlCfGlassHstV.setRecipeId(hisOut.getMachinerecipename());

//        MesCtProductlocationhist mesCtProductlocationhist=new MesCtProductlocationhist();
            String hql = "FROM MesCtProductlocationhist WHERE EVENTNAME='ProductOut' AND TRACELEVEL<>'P' and  LINKKEY='" + hisOut.getLinkkey() + "' and PRODUCTNAME='" + hisOut.getProductname() + "' and SUBFACTORYNAME='" + hisOut.getShipid() + "' order by EVENTTIME ASC";
            String chamberPath = "";
            String chamberTime = "";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<MesCtProductlocationhist> mesCtProductlocationhistList = mesdao.list(MesCtProductlocationhist.class, hql.toString());
            if (mesCtProductlocationhistList.size() > 0) {
                for (int i = 0; i < mesCtProductlocationhistList.size(); i++) {
                    chamberPath = chamberPath + mesCtProductlocationhistList.get(i).getUnitname();
                    chamberTime = chamberTime + sdf.format(mesCtProductlocationhistList.get(i).getEventtime().getTime());
                }
            }
            etlCfGlassHstV.setChamberPath(chamberPath);
            etlCfGlassHstV.setChamberTime(chamberTime);
            etlCfGlassHstV.setTaShift(hisOut.getEventname());
            etlCfGlassHstV.setRunMode(hisOut.getRunmode());
            etlCfGlassHstV.setOwnerId(hisOut.getOwnerid());
            etlCfGlassHstV.setOwnerType(hisOut.getOwnertype());
            //8
            etlCfGlassHstV.setCstId(hisOut.getCarriername());
            etlCfGlassHstV.setSlotNo(String.valueOf(hisOut.getPosition()));
            etlCfGlassHstV.setCstIdMoveIn(hisOut.getCarriername());
            etlCfGlassHstV.setSlotNoMoveIn(String.valueOf(hisOut.getPosition()));
            //13-14
            etlCfGlassHstV.setPortIdMoveIn(hisOut.getProcessflowname());
            etlCfGlassHstV.setPortIdMoveOut(hisOut.getEventuser());
            etlCfGlassHstV.setMesLinkKry(hisOut.getLinkkey());
            if (hisOut.getProductspecname().length() >= 7) {
                if ("OC".equals(hisOut.getFactoryname())) {
                    if ("L".equals(hisOut.getProductspecname().substring(0, 1))) {
                        etlCfGlassHstV.setPanelSize(hisOut.getProductspecname().substring(2, 7));
                    } else {
                        etlCfGlassHstV.setPanelSize(hisOut.getProductspecname().substring(1, 6));
                    }
                } else {
                    etlCfGlassHstV.setPanelSize(hisOut.getProductspecname().substring(2, 7));
                }
            }
            //19-21
            etlCfGlassHstV.setHoldFlag(hisOut.getProductholdstate());
            etlCfGlassHstV.setHoldDept(hisOut.getHolddepartment());
            etlCfGlassHstV.setQtapId(hisOut.getQtapgroupname());
            etlCfGlassHstV.setLotPriority(String.valueOf(hisOut.getPriority()));
            etlCfGlassHstV.setGlassGrade(hisOut.getProductgrade());
            etlCfGlassHstV.setGlassJudge(hisOut.getProductjudge());
            etlCfGlassHstV.setPanelGrades(hisOut.getPanelgrades());
            etlCfGlassHstV.setPanelOxInfo(hisOut.getPaneloxs());
            etlCfGlassHstV.setNgMark(hisOut.getNgmark());
            etlCfGlassHstV.setPanelQty(Long.valueOf(getNum(hisOut.getPaneltotalqty())));
            etlCfGlassHstV.setPanelQtyGood(Long.valueOf(getNum(hisOut.getGoodpanelgradeqty())));
            etlCfGlassHstV.setDefectCode(hisOut.getDefectcodes());
            etlCfGlassHstV.setReworkCnt(hisOut.getReworkcount());
            etlCfGlassHstV.setRecycleCnt(hisOut.getRecyclecount());
            //32
            etlCfGlassHstV.setExpNo(hisOut.getExpno());
            etlCfGlassHstV.setTecnId(hisOut.getTecnname());
            etlCfGlassHstV.setMaskId(hisOut.getMaskname());
            etlCfGlassHstV.setMtrlPartNo(hisOut.getTftpolpartno());
            etlCfGlassHstV.setMtrlLotNo(hisOut.getTftpollotname());


        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_PROCESS_GP2ORA_GLASS_HST,e);
        }
        return etlCfGlassHstV;
    }


    private Long getNum(String str) {
        Long num;
        try {
            num = Long.valueOf(str);
            return num;
        } catch (Exception e) {
//            logger.info("FormatException: BigDecimal[{}] cannot be cast to NUMBER", str);
            num = Long.valueOf(-1);
            return num;
        }
    }
}
